/*
* @author:Cao Tongqin
* */

var password = "" ;
var repeatPassword = "" ;
var accountNumber = "" ;
// var xml = XMLHttpRequest() ;
var registerState = -1 ;
window.onload =function () {

};

function enterPassword(inp) {
    password = inp.value ;
    checkPassLength() ;
}

function enterAccount(e) {
    accountNumber = e.value ;
    console.log(e.value) ;
}

/*
* 判断密码长度是否为8-16位*/
function checkPassLength() {
    if (password.length < 8 || password.length > 16){
        var dom = window.document.getElementById("leastPassword") ;
        dom.style.display = "block" ;
    }
    else {
        var dom = window.document.getElementById("leastPassword") ;
        dom.style.display = "none" ;
    }
    showLevel(checkStrong(password)) ;
}

/*
* 用于判断展示密码强度等级*/
function showLevel(inp) {
    var easy = window.document.getElementById("easy") ;
    var ordinary = window.document.getElementById("ordinary") ;
    var difficult = window.document.getElementById("difficult") ;
    var dom = window.document.getElementById("passLevel") ;
    if (inp==1){
        easy.style.background = "rgb(255,17,17)" ;
        dom.innerText = "低" ;
    }
    else if(inp==2){
        easy.style.background = ordinary.style.background = "rgb(228,162,37)" ;
        dom.innerText = "中" ;
    }
    else if (inp==3 || inp==4){
        easy.style.background = ordinary.style.background = difficult.style.background = "rgb(28,238,133)" ;
        dom.innerText = "高" ;
    }
    else{
        easy.style.background = ordinary.style.background = difficult.style.background = "#C0C0C0" ;
        dom.innerText = "无"
    }

}


function repeatPass(inp) {
    repeatPassword = inp.value ;

    /*
    * 判断再次输入的密码与第一次是否相同*/
    if (repeatPassword != password){
        var dom = window.document.getElementById("passNotMatch") ;
        dom.style.display = "block" ;
    }
    else {
        var dom = window.document.getElementById("passNotMatch") ;
        dom.style.display = "none" ;
        progress++ ;
    }
}

/*
* 用来判断char的类型*/
function charMode(inp) {
    if (inp>=48 && inp<=57) //数字
        return 1 ;
    else if(inp>=65 && inp<=90) //大写
        return 2 ;
    else if(inp>=97 && inp<=122) //小写
        return 4 ;
    else
        return 8 ;
}

/*
* 计算密码模式*/
function bitTotal(inp) {
    level = 0 ;
    for (var i=0; i<4; i++){
        if(inp & 1)
            level++ ;
        inp>>>=1 ;
    }
    return level ;
}


/*
* 返回强度等级*/
function checkStrong(inp) {
    mode = 0 ;
    if (inp.length<8 || inp.length>16)
        return 0 ;
    for (var i=0; i<inp.length; i++){
        //密码模式
        mode |= charMode(inp.charCodeAt(i)) ;
    }
    return bitTotal(mode) ;
}

/*
* 判断checkbox是否触发点击事件*/
function agreeContract() {
    var firstDom = window.document.getElementById("progressOne") ;
    var forthDom = window.document.getElementById("progressFour") ;
    firstDom.style.display = "none" ;
    forthDom.style.display = "block" ;
}

function finishRegister() {
    // SetCookie("account",accountNumber,1) ;
    // sendCookie();
    document.cookie = accountNumber ;
    window.location.href = "../registerSuc/registerSuc.html";
}

// function SetCookie(cookieName,cookieValue,nDays) {
//     /*当前日期*/
//     let today = new Date() ;
//     /*Cookie过期时间*/
//     let expire = new Date() ;
//     /*如果未设置nDays参数或者nDays取值为0，默认为1*/
//     if(nDays == null || nDays == 0){
//         nDays = 1 ;
//     }
//     /*计算cookie的过期时间*/
//     expire.setTime(today.getTime()+3600000*24*nDays) ;
//     /*设置Cookie的值*/
//     document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString() ;
// }

function sendCookie() {
    console.log("sendCookies",accountNumber) ;
    // document.cookie('account',accountNumber) ;
    $.cookie('account',"123456789") ;
}

function sendAjax() {
    $.ajax({
        type:"GET",
        url:"http://localhost:8080/DarkCircle/register ",
        data:{
            "account":accountNumber,
            "password":password,
        },
        async:true,//异步
        cache:true,//浏览器缓存被请求页面
        dataType:"json", //返回浏览器的数据类型
        success:function (data) {
            if (data == 0){
                SetCookie("account",accountNumber,1) ;
               finishRegister() ;
            }
            else{
                alert("注册失败，请重新尝试") ;
            }
        }
    })
}